package com.zhu.salesmanagement.network;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.provider.SalesSettings;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class PostData {

    public static Object[] getNewAddStores(String eid, String uuid, Context context){
        List<Object> list = new ArrayList<Object>();
        Cursor cursor = null;
        try{
            cursor = context.getContentResolver().query(
                    SalesSettings.MyAddStoresColumns.CONTENT_URI, null, null, null, null);
            final int nameIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.NAME);
            final int addressIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.ADDRESS);
            final int contactIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.CONTACT);
            final int mobileIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.MOBILE);
            final int photoIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.PHOTO);
            final int latIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.LAT);
            final int lngIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.LNG);
            final int typeIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.TYPE);
            while (cursor.moveToNext()) {
                Map map = new HashMap<String, Object>();
                map.put("sCustomerName", cursor.getString(nameIndex));
                map.put("sCustomerAddress", cursor.getString(addressIndex));
                map.put("sCustomerContractor", cursor.getString(contactIndex));
                map.put("sTel", cursor.getString(mobileIndex));
                map.put("sDealtEmployeeID", eid);
                map.put("iVisitCategory", Integer.parseInt(cursor.getString(typeIndex)));
                map.put("decLongitude", Double.parseDouble(cursor.getString(lngIndex)));
                map.put("decLatitude", Double.parseDouble(cursor.getString(latIndex)));
                map.put("imgPhoto", cursor.getBlob(photoIndex));
                list.add(map);
            }
        }catch(Exception e){
        	e.printStackTrace();
            if(Actions.DEBUG){
            	Log.e("zhu","getNewAddStores error");
            }
        }finally{
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }
        if(list.size() > 0){
        	return new Object[]{ eid, uuid, list };
        }else{
        	return null;
        }
    }

    public static void deleteAllNewAddedStores(Context context){
    	context.getContentResolver().delete(SalesSettings.MyAddStoresColumns.CONTENT_URI_NO_NOTIFICATION, null, null);
    }

    public static Object[] getCustomerClosedList(String eid, String uuid, Context context){
    	 List<String> list = new ArrayList<String>();
    	Cursor cursor = null;
        try{
            cursor = context.getContentResolver().query(
                    SalesSettings.VisitedStoresColumns.CONTENT_URI, null, "close = 1", null, null);
            final int cidIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.STOREID);
            while (cursor.moveToNext()) {
            	list.add(cursor.getString(cidIndex));
            }
        }catch(Exception e){
        	e.printStackTrace();
            if(Actions.DEBUG){
            	Log.e("zhu","getNewAddStores error");
            }
        }finally{
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }
        if(Actions.DEBUG){
        	Log.e("zhu","getCustomerClosedListSize=" + list.size());
        }
        if(list.size() > 0){
        	return new Object[]{ eid, uuid, list };
        }else{
        	return null;
        }
    }

    public static void deleteAllRemindsData(Context context){
    	String where = "";
    	Cursor cursor = null;
        try{
            cursor = context.getContentResolver().query(
                    SalesSettings.RemindsColumns.CONTENT_URI, new String[]{ SalesSettings.RemindsColumns.EVENTID }, null, null, null);
            final int eventIdIndex = cursor.getColumnIndexOrThrow(SalesSettings.RemindsColumns.EVENTID);
            while (cursor.moveToNext()) {
                String eid = cursor.getString(eventIdIndex);
                if(eid != null && eid.length() > 0){
                    if (where.length() == 0) {
                        where += SalesSettings.RemindsColumns._ID + " = " + eid;
                    } else {
                        where += " or " + SalesSettings.RemindsColumns._ID + " = " + eid;
                    }
                }
            }
        }catch(Exception e){
        	e.printStackTrace();
            if(Actions.DEBUG){
            	Log.e("zhu","getNewAddStores error");
            }
        }finally{
            if(cursor != null){
                cursor.close();
                cursor = null;
            }
        }
        context.getContentResolver().delete(SalesSettings.RemindsColumns.CONTENT_URI_NO_NOTIFICATION, null, null);
        if(where.length() > 0){
        	if(Actions.DEBUG){
        		Log.e("zhu","where="+where);
        	}
        	Actions.deleteEvent(context, where);	
        }
    }
//    public static Object[] getVistedTaskList(String eid, String uuid, Context context){
//    	List<Object> list = new ArrayList<Object>();
//    	Cursor cursor = null;
//    	 try{
//             cursor = context.getContentResolver().query(SalesSettings.VisitedStoresColumns.CONTENT_URI, null, "visit = 1", null, null);
//             final int idIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns._ID);
//             final int storeidIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.STOREID);
//             final int itemnoIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.ITEMNO);
//             final int checkintimeIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.CHECKIN_TIME);
//             final int checkouttimeIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.CHECKOUT_TIME);
//             final int visitedIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.VISITSTATUS);
//             final int taskidIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.TASKID);
//             final int taskdateIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.TASTDATE);
//             final int aeidIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.AEMPLOYEEID);
//             final int checkinLatIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.CHECKIN_LAT);
//             final int checkinLngIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.CHECKIN_LNG);
//             final int checkoutLatIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.CHECKOUT_LAT);
//             final int checkoutLngIndex = cursor.getColumnIndexOrThrow(SalesSettings.VisitedStoresColumns.CHECKOUT_LNG);
//
//             while (cursor.moveToNext()) {
//                 Map map = new HashMap<String, Object>();
//                 map.put("dActualDate", new Date(Long.parseLong(cursor.getString(taskdateIndex))));
//                 map.put("dSignInTime", new Date(Long.parseLong(cursor.getString(checkintimeIndex))));
//                 map.put("decSignInLongitude", Double.parseDouble(cursor.getString(checkinLngIndex)));
//                 map.put("decSignInLatitude", Double.parseDouble(cursor.getString(checkinLatIndex)));
//                 map.put("dSignOutTime", new Date(Long.parseLong(cursor.getString(checkouttimeIndex))));
//                 map.put("decSignOutLongitude", Double.parseDouble(cursor.getString(checkoutLngIndex)));
//                 map.put("decSignOutLatitude", Double.parseDouble(cursor.getString(checkoutLatIndex)));
//                 map.put("sVisitEmployeeID", cursor.getString(aeidIndex));
//                 map.put("sCustomerID", cursor.getString(storeidIndex));
//                 map.put("sDatingTaskID", cursor.getString(taskidIndex));
//                 list.add(map);
//             }
//         }catch(Exception e){
//         	e.printStackTrace();
//             if(Actions.DEBUG){
//             	Log.e("zhu","getVistedTaskList error");
//             }
//         }finally{
//             if(cursor != null){
//                 cursor.close();
//                 cursor = null;
//             }
//         }
//         if(list.size() > 0){
//         	return new Object[]{ eid, uuid, list };
//         }else{
//         	return null;
//         }
//    }
//
//    public static void deleteVistedTaskList(Context context){
//    	context.getContentResolver().delete(SalesSettings.VisitedStoresColumns.CONTENT_URI, "visit = 1", null);
//    }
}
